package com.systop.common.modules.dept.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.rbcs.base.model.Item;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.Constants;
import com.systop.core.model.BaseModel;

/**
 * 部门实体类
 * 
 * @author Nice
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "depts", uniqueConstraints = {})
public class Dept extends BaseModel {

	/** id */
	private Integer id;

	/**
	 * 部门描述
	 */
	private String descn;

	/**
	 * 部门名称
	 */
	private String name;

	/**
	 * 部门类别
	 */
	private String type;

	/**
	 * 部门编号规则：两位数字，从1自动排；
	 */
	private String serialNo;
	
	/**
	 * 预算类型 月度：month 季度:qtr 
	 */
	private String budgetType;

	/**
	 * 上级部门
	 */
	private Dept parent;
	
	/**
	 * 下级部门
	 */
	private Set<Dept> childDepts = new HashSet<Dept>(0);

	/**
	 * 部门下用户
	 */
	private Set<User> users = new HashSet<User>(0);

	/**
	 * 部门涉及到预算科目
	 */
	private List<Item> items = new ArrayList<Item>(0);
	
	//是否末端,默认为末端部门
	private String isLeaf = Constants.YES;

	@Transient
	public boolean getHasChild() {
		return this.getChildDepts().size() > 0;
	}

	/**
	 * 缺省构造
	 */
	public Dept() {
	}

	@Id
	@GeneratedValue(generator = "hibseq")
	@GenericGenerator(name = "hibseq", strategy = "hilo")
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "descn")
	public String getDescn() {
		return this.descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@Column(name = "name")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Column(name = "serial_no")
	public String getSerialNo() {
		return this.serialNo;
	}

	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}

	@Column(name = "budget_type")
	public String getBudgetType() {
		return budgetType;
	}

	public void setBudgetType(String budgetType) {
		this.budgetType = budgetType;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id")
	public Dept getParent() {
		return this.parent;
	}

	public void setParent(Dept parent) {
		this.parent = parent;
	}

	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "parent")
	public Set<Dept> getChildDepts() {
		return this.childDepts;
	}

	public void setChildDepts(Set<Dept> childDepts) {
		this.childDepts = childDepts;
	}

	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "dept")
	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	@ManyToMany(cascade = {}, fetch = FetchType.LAZY, targetEntity = Item.class)
	@JoinTable(name = "dept_item", joinColumns = { @JoinColumn(name = "dept_id") }, inverseJoinColumns = { @JoinColumn(name = "item_id") })
	@OrderBy("code")
	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	@Column(name = "is_leaf")
	public String getIsLeaf() {
		return isLeaf;
	}

	public void setIsLeaf(String isLeaf) {
		this.isLeaf = isLeaf;
	}
}
